﻿


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using HBase;
using hCommand;
using Net.http;
using Persistence;
using Persistence.sqlserver;

namespace WebApi
{
    [FormAuthenticationFilter]

    [RoutePrefix("doo")]
    public class DooController : BaseApiController
    {
        IDooServer dooServer = WMSDalFactray.getDal<IDooServer>();

        public IItemServer iitemServer = WMSDalFactray.getDal<IItemServer>();
        [HttpPost, Route("FindPage")]
        public BaseResult FindPage([FromBody] DoDto dto)
        {
            Page<WhDoo> info = dooServer.QueryReceiptOutPage(dto);
            return BaseResult.Ok(info);
        }



        [HttpPost, Route("batchDelete"), ControlName("出库单删除")]
        public BaseResult batchDelete([FromBody] List<WhDoo> list)
        {
            foreach (WhDoo receiptOut in list)
            {
                WhDoo model = dooServer.getById<WhDoo>(receiptOut.id);
                if (model.status != 1)
                {
                    return BaseResult.Error("出库单非新建待执行状态无法删除!");
                }
                dooServer.DeleteDetailByReceptId(receiptOut.id);
                dooServer.delete(model);
             
            }
            return BaseResult.Ok("ok");
        }

        [HttpPost, Route("Save"), ControlName("出库单保存")]
        public BaseResult Save([FromBody] WhDoo receiptOut)
        {

            if (receiptOut.id == 0)
            {

                receiptOut.status = 1;
                dooServer.insertNotNull(receiptOut);
                return BaseResult.Ok(receiptOut);
            }
            else
            {
                dooServer.updateNotNull(receiptOut);
                return BaseResult.Ok(receiptOut);
            }

        }
        [HttpPost, Route("getDetials")]
        public BaseResult getDetials([FromBody] WhDoo dto)
        {
            List<WhDooDet> info = dooServer.getDetials(dto.id);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("saveDoo"), ControlName("保存出库单及明显")]
        public BaseResult saveDoo([FromBody] DooDto dto)
        {
            WhDoo doo = dto.main;
            if (doo.id == 0)
            {
                doo.status = 1;
            }
            else
            {
                WhDoo receiptOut = dooServer.getById<WhDoo>(doo.id);
                if (receiptOut.status != 1)
                {
                    return BaseResult.Error("出库单非新建待执行状态无法修改!");
                }
            }
            List<WhDooDet> whDooDets = new List<WhDooDet>();
            foreach (var item in dto.detList)
            {
                if (item.planCount > 0)
                {
                    whDooDets.Add(item);
                }
            }
            dto.detList = whDooDets;
            dooServer.saveOne2Many(dto);
            return BaseResult.Ok(dto);
        }




        [HttpPost, Route("saveDetials"), ControlName("出库单明细保存")]
        public BaseResult saveDetials([FromBody] DoAddDto dto)
        {
            if (dto.id == 0)
            {
                WhDoo receiptOut1 = new WhDoo();
                dto.id = receiptOut1.id;

                receiptOut1.status = 1;
                receiptOut1.stn = dto.stn;
                dooServer.insertNotNull(receiptOut1);
                //return BaseResult.Error("请先添加主表信息");
            }
            WhDoo receiptOut = dooServer.getById<WhDoo>(dto.id);
            if (receiptOut.status != 1)
            {
                return BaseResult.Error("出库单非新建待执行状态无法修改!");
            }
            List<WhDooDet> detailList = dooServer.GetReceiptOutDetail(dto);
            string[] ids = dto.items.Split(',');
            foreach (string id in ids)
            {
                bool same = false;
                foreach (var soDetail in detailList)
                {
                    if (soDetail.itemId == long.Parse(id.Split('|')[0]))
                    {
                        same = true;
                    }
                }
                if (!same)
                {
                    WhDooDet detail = new WhDooDet();

                    detail.itemId = long.Parse(id);
                    detail.pid = dto.id;
                    detail.stn = receiptOut.stn;
                    detail.planCount = 0;
                    dooServer.insert(detail);
                }
            }
            return BaseResult.Ok(dto);
        }

     

        [HttpPost, Route("saveDetialsByBatch"), ControlName("批次出库明细保存")]//批次出库
        public BaseResult saveDetialsByBatch([FromBody] DoAddDto dto)
        {
            if (dto.id == 0)
            {
                return BaseResult.Error("请先添加主表信息");
            }
            WhDoo receiptOut = dooServer.getById<WhDoo>(dto.id);
            if (receiptOut.status != 1)
            {
                return BaseResult.Error("出库单非新建待执行状态无法修改!");
            }
            List<WhDooDet> detailList = dooServer.GetReceiptOutDetail(dto);
            string[] ids = dto.items.Split(',');

            //重复批次验证
            foreach (var id in ids)
            {
                bool same = false;
                foreach (var soDetail in detailList)
                {
                    if (soDetail.batchId == long.Parse(id))
                    {
                        //return BaseResult.Error(500, "不能重复添加批次");
                        same = true;
                    }
                }
                if (!same)
                {
                    WhDooDet detail = new WhDooDet();


                    detail.pid = dto.id;
                    detail.stn = receiptOut.stn;
                    detail.planCount = 0;
                    detail.batchId = long.Parse(id);
                    WhBatch b = dooServer.getById<WhBatch>(detail.batchId);

                    detail.itemId = (long)b.itemId;
                    detail.batchNo = b.batchNo;
                    dooServer.insert(detail);
                }
            }
            return BaseResult.Ok("ok");
        }

        [HttpPost, Route("updateDetials"), ControlName("出库单明细修改")]
        public BaseResult updateDetials([FromBody] WhDooDet dto)
        {
            WhDoo receiptOut = dooServer.getById<WhDoo>(dto.pid);
            if (receiptOut.status != 1)
            {
                return BaseResult.Error("出库单非新建待执行状态无法删除!");
            }
            dooServer.updateNotNull(dto);
            return BaseResult.Ok("ok");
        }


        [HttpPost, Route("deleteDetial"), ControlName("出库单明细删除")]
        public BaseResult deleteDetial([FromBody] WhDooDet dto)
        {
            TransRes res = WMSTransactionFacade.doDooDeleteDetEngine(dto);
            if (res.s)
            {
                return BaseResult.Ok("生成成功");
            }
            else
            {
                return BaseResult.Error(res.msg);
            }

            //WhDoo receiptOut = dooServer.getById<WhDoo>(dto.pid);
            //if (receiptOut.status != 1)
            //{
            //    return BaseResult.Error("出库单非新建待执行状态无法修改!");
            //}
            //dooServer.delete<WhDooDet>(dto.id);

            //dooServer.updateNotNull(receiptOut);
            //return BaseResult.Ok("ok");
        }

        [HttpPost, Route("selectCsd"), ControlName("明显待选查询")]
        public BaseResult selectCsd([FromBody] WhDooDet dto) {
            var res = dooServer.getCsdListForSelect(dto); 
            return BaseResult.Ok(res);
        }

   

        //生成任务(物料,销售,生产出库)
        [HttpPost, Route("otherOutGenerate"), ControlName("物料出库生成任务")]
        public BaseResult OtherOutGenerate([FromBody] WhDoo whReceipt)
        {
            TransRes res = WMSTransactionFacade.doDooEngine(whReceipt);
            if (res.s)
            {
                return BaseResult.Ok("生成成功");
            }
            else
            {
                return BaseResult.Error(res.msg);
            }


        }

        [HttpPost, Route("finsh")]
        public BaseResult finsh([FromBody] WhDoo dto) {
            dooServer.finshDo(dto);
            return BaseResult.Ok("成功");
        }
        //生成任务(物料,销售,生产出库)
        [HttpPost, Route("csds"), ControlName("选择明显")]
        public BaseResult csds([FromBody] WhDoo whReceipt) {
          List<CoreStockDet> list = dooServer.getCsdList(whReceipt);    
            return BaseResult.Ok(list);
            


        }

        [HttpPost, Route("Preview")]
        public BaseResult Preview([FromBody] WhDoo receiptOut)
        {
            if (receiptOut == null)
            {
                return BaseResult.Error("没有数据无法打印");
            }
            OutPrint outPrint = new OutPrint();

            string currentPath = System.AppDomain.CurrentDomain.BaseDirectory;
            string html = getPrintUtf8("stockDetail.html");
            List<WhDoo> list = dooServer.GetAllList(receiptOut.id);
            var stockOutList = list;
            var stockOutDList = dooServer.GetOutDetials(list[0].id);
            outPrint.html = html;
            outPrint.outList = list;
            outPrint.outDetailList = stockOutDList;
            return BaseResult.Ok(outPrint);
        }



        [HttpPost, Route("GetWhDoiDetList")]
        public BaseResult GetWhDoiDetList([FromBody] DoDto dto)
        {
            List<WhDoExcel> info = dooServer.GetExportList(dto);

            return BaseResult.Ok(info);
        }

        //出库明细
        [HttpPost, Route("GetReceiptOutList")]
        public BaseResult GetReceiptOutList([FromBody] DoDto dto)
        {

            var outReceiptDetail = dooServer.GetAll<WhDooDet>(" AND id=@outDetailId", new { outDetailId = dto.outDetailId }).FirstOrDefault();
            if (outReceiptDetail == null)
            {
                return BaseResult.Error("没有对应绑定的出库单明细");
            }
            var outReceipt = dooServer.GetAll<WhDoo>(" AND id=@outId", new { outId = outReceiptDetail.pid }).FirstOrDefault();
            return BaseResult.Ok(outReceipt);
        }


    }
}